package com.canbot.u05.activity.presidemode.newcode.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.newcode.bean.VoiceBean;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IItemClick;
import com.canbot.u05.control.DataUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NChoiceMusicAdapter extends RecyclerView.Adapter<NChoiceMusicAdapter.NChoiceMusicViewHolder> {
    private final Context mContext;
    private List<VoiceBean> mTaskList;
    private LayoutInflater layoutInflater;
    private IItemClick mIItemClick;
    private int mPrevCheckPos = -1;

    public NChoiceMusicAdapter(List<VoiceBean> taskList, Context context, IItemClick iItemClick) {
        this.mTaskList = taskList;
        if (mTaskList == null) {
            mTaskList = new ArrayList<>();
        }
        this.mContext = context;
        layoutInflater = LayoutInflater.from(mContext);
        this.mIItemClick = iItemClick;
    }

    @NonNull
    @Override
    public NChoiceMusicViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_choice_music, viewGroup, false);
        return new NChoiceMusicViewHolder(view);
    }

    private static final String TAG = "NChoiceMusicAdapter";

    @Override
    public void onBindViewHolder(@NonNull NChoiceMusicViewHolder viewHolder, final int postion) {
        final VoiceBean musicItemBean = mTaskList.get(postion);
        if (musicItemBean != null) {
            viewHolder.selectedMusic.setVisibility(musicItemBean.isSelect() ?
                    View.VISIBLE : View.INVISIBLE);
            viewHolder.tvMusicName.setText(musicItemBean.content);
            viewHolder.tvMusicTime.setText(DataUtil.formatTime(musicItemBean.getTime()));
        }
        viewHolder.tvMusicName.setOnClickListener(v -> {
            if (mIItemClick != null) {
                mIItemClick.onClick(postion, v);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mTaskList.size();
    }

    public class NChoiceMusicViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_music_name)
        TextView tvMusicName;
        @BindView(R.id.tv_music_time)
        TextView tvMusicTime;
        @BindView(R.id.selected_music)
        ImageView selectedMusic;

        public NChoiceMusicViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
